// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.Lua.LuaEventManager;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.core.raknet.UdpEngine;
import zombie.debug.DebugLog;
import zombie.inventory.ItemContainer;
import zombie.iso.objects.IsoLightSwitch;
import zombie.network.GameClient;
import zombie.network.PacketTypes;
import zombie.network.ServerMap;
import zombie.network.WorldItemTypes;

public final class ObjectsSyncRequests {
    public static final short ClientSendChunkHashes = 1;
    public static final short ServerSendGridSquareHashes = 2;
    public static final short ClientSendGridSquareRequest = 3;
    public static final short ServerSendGridSquareObjectsHashes = 4;
    public static final short ClientSendObjectRequests = 5;
    public static final short ServerSendObject = 6;
    public ArrayList<ObjectsSyncRequests.SyncIsoChunk> requestsSyncIsoChunk;
    public ArrayList<ObjectsSyncRequests.SyncIsoGridSquare> requestsSyncIsoGridSquare;
    public ArrayList<ObjectsSyncRequests.SyncIsoObject> requestsSyncIsoObject;
    public long timeout = 1000L;

    public ObjectsSyncRequests(boolean isClient) {
        if (isClient) {
            this.requestsSyncIsoChunk = new ArrayList();
            this.requestsSyncIsoGridSquare = new ArrayList();
            this.requestsSyncIsoObject = new ArrayList();
        } else {
            this.requestsSyncIsoGridSquare = new ArrayList();
        }
    }

    static int getObjectInsertIndex(long[] long2, long[] long1, long long0) {
        if (long0 == long1[0]) {
            return 0;
        } else {
            for (int int0 = 0; int0 < long2.length; int0++) {
                if (long2[int0] == long0) {
                    return -1;
                }
            }

            int int1 = 0;

            for (int int2 = 0; int2 < long1.length; int2++) {
                if (int1 < long2.length && long1[int2] == long2[int1]) {
                    int1++;
                }

                if (long1[int2] == long0) {
                    return int1;
                }
            }

            return -1;
        }
    }

    public void putRequestSyncIsoChunk(IsoChunk ch) {
        if (!GameClient.bClient || zombie.SystemDisabler.doWorldSyncEnable) {
            ObjectsSyncRequests.SyncIsoChunk syncIsoChunk = new ObjectsSyncRequests.SyncIsoChunk();
            syncIsoChunk.x = ch.wx;
            syncIsoChunk.y = ch.wy;
            syncIsoChunk.hashCodeObjects = ch.getHashCodeObjects();
            syncIsoChunk.reqTime = 0L;
            syncIsoChunk.reqCount = 0;
            synchronized (this.requestsSyncIsoChunk) {
                this.requestsSyncIsoChunk.add(syncIsoChunk);
            }
        }
    }

    public void putRequestSyncItemContainer(ItemContainer container) {
        if (container != null && container.parent != null && container.parent.square != null) {
            this.putRequestSyncIsoGridSquare(container.parent.square);
        }
    }

    public void putRequestSyncIsoGridSquare(IsoGridSquare sq) {
        if (sq != null) {
            ObjectsSyncRequests.SyncIsoGridSquare syncIsoGridSquare = new ObjectsSyncRequests.SyncIsoGridSquare();
            syncIsoGridSquare.x = sq.x;
            syncIsoGridSquare.y = sq.y;
            syncIsoGridSquare.z = sq.z;
            syncIsoGridSquare.reqTime = 0L;
            syncIsoGridSquare.reqCount = 0;
            synchronized (this.requestsSyncIsoGridSquare) {
                if (!this.requestsSyncIsoGridSquare.contains(sq)) {
                    this.requestsSyncIsoGridSquare.add(syncIsoGridSquare);
                } else {
                    DebugLog.log("Warning: [putRequestSyncIsoGridSquare] Tryed to add dublicate object.");
                }
            }
        }
    }

    public void sendRequests(UdpConnection connection) {
        if (zombie.SystemDisabler.doWorldSyncEnable) {
            if (this.requestsSyncIsoChunk != null && this.requestsSyncIsoChunk.size() != 0) {
                ByteBufferWriter byteBufferWriter0 = connection.startPacket();
                PacketTypes.PacketType.SyncObjects.doPacket(byteBufferWriter0);
                byteBufferWriter0.putShort((short)1);
                ByteBuffer byteBuffer0 = byteBufferWriter0.bb;
                int int0 = byteBuffer0.position();
                byteBufferWriter0.putShort((short)0);
                int int1 = 0;
                synchronized (this.requestsSyncIsoChunk) {
                    for (int int2 = this.requestsSyncIsoChunk.size() - 1; int2 >= 0; int2--) {
                        ObjectsSyncRequests.SyncIsoChunk syncIsoChunk = (ObjectsSyncRequests.SyncIsoChunk)this.requestsSyncIsoChunk.get(int2);
                        if (syncIsoChunk.reqCount > 3) {
                            this.requestsSyncIsoChunk.remove(int2);
                        } else {
                            if (syncIsoChunk.reqTime == 0L) {
                                syncIsoChunk.reqTime = System.currentTimeMillis();
                                int1++;
                                byteBuffer0.putInt(syncIsoChunk.x);
                                byteBuffer0.putInt(syncIsoChunk.y);
                                byteBuffer0.putLong(syncIsoChunk.hashCodeObjects);
                                syncIsoChunk.reqCount++;
                            }

                            if (System.currentTimeMillis() - syncIsoChunk.reqTime >= this.timeout) {
                                syncIsoChunk.reqTime = System.currentTimeMillis();
                                int1++;
                                byteBuffer0.putInt(syncIsoChunk.x);
                                byteBuffer0.putInt(syncIsoChunk.y);
                                byteBuffer0.putLong(syncIsoChunk.hashCodeObjects);
                                syncIsoChunk.reqCount++;
                            }

                            if (int1 >= 5) {
                                break;
                            }
                        }
                    }
                }

                if (int1 == 0) {
                    GameClient.connection.cancelPacket();
                    return;
                }

                int int3 = byteBuffer0.position();
                byteBuffer0.position(int0);
                byteBuffer0.putShort((short)int1);
                byteBuffer0.position(int3);
                PacketTypes.PacketType.SyncObjects.send(GameClient.connection);
            }

            if (this.requestsSyncIsoGridSquare != null && this.requestsSyncIsoGridSquare.size() != 0) {
                ByteBufferWriter byteBufferWriter1 = connection.startPacket();
                PacketTypes.PacketType.SyncObjects.doPacket(byteBufferWriter1);
                byteBufferWriter1.putShort((short)3);
                ByteBuffer byteBuffer1 = byteBufferWriter1.bb;
                int int4 = byteBuffer1.position();
                byteBufferWriter1.putShort((short)0);
                int int5 = 0;
                synchronized (this.requestsSyncIsoGridSquare) {
                    for (int int6 = 0; int6 < this.requestsSyncIsoGridSquare.size(); int6++) {
                        ObjectsSyncRequests.SyncIsoGridSquare syncIsoGridSquare = (ObjectsSyncRequests.SyncIsoGridSquare)this.requestsSyncIsoGridSquare
                            .get(int6);
                        if (syncIsoGridSquare.reqCount > 3) {
                            this.requestsSyncIsoGridSquare.remove(int6);
                            int6--;
                        } else {
                            if (syncIsoGridSquare.reqTime == 0L) {
                                syncIsoGridSquare.reqTime = System.currentTimeMillis();
                                int5++;
                                byteBuffer1.putInt(syncIsoGridSquare.x);
                                byteBuffer1.putInt(syncIsoGridSquare.y);
                                byteBuffer1.put((byte)syncIsoGridSquare.z);
                                syncIsoGridSquare.reqCount++;
                            }

                            if (System.currentTimeMillis() - syncIsoGridSquare.reqTime >= this.timeout) {
                                syncIsoGridSquare.reqTime = System.currentTimeMillis();
                                int5++;
                                byteBuffer1.putInt(syncIsoGridSquare.x);
                                byteBuffer1.putInt(syncIsoGridSquare.y);
                                byteBuffer1.put((byte)syncIsoGridSquare.z);
                                syncIsoGridSquare.reqCount++;
                            }

                            if (int5 >= 100) {
                                break;
                            }
                        }
                    }
                }

                if (int5 == 0) {
                    GameClient.connection.cancelPacket();
                    return;
                }

                int int7 = byteBuffer1.position();
                byteBuffer1.position(int4);
                byteBuffer1.putShort((short)int5);
                byteBuffer1.position(int7);
                PacketTypes.PacketType.SyncObjects.send(GameClient.connection);
            }

            if (this.requestsSyncIsoObject != null && this.requestsSyncIsoObject.size() != 0) {
                ByteBufferWriter byteBufferWriter2 = connection.startPacket();
                PacketTypes.PacketType.SyncObjects.doPacket(byteBufferWriter2);
                byteBufferWriter2.putShort((short)5);
                ByteBuffer byteBuffer2 = byteBufferWriter2.bb;
                int int8 = byteBuffer2.position();
                byteBufferWriter2.putShort((short)0);
                int int9 = 0;
                synchronized (this.requestsSyncIsoObject) {
                    for (int int10 = 0; int10 < this.requestsSyncIsoObject.size(); int10++) {
                        ObjectsSyncRequests.SyncIsoObject syncIsoObject = (ObjectsSyncRequests.SyncIsoObject)this.requestsSyncIsoObject.get(int10);
                        if (syncIsoObject.reqCount > 3) {
                            this.requestsSyncIsoObject.remove(int10);
                            int10--;
                        } else {
                            if (syncIsoObject.reqTime == 0L) {
                                syncIsoObject.reqTime = System.currentTimeMillis();
                                int9++;
                                byteBuffer2.putInt(syncIsoObject.x);
                                byteBuffer2.putInt(syncIsoObject.y);
                                byteBuffer2.put((byte)syncIsoObject.z);
                                byteBuffer2.putLong(syncIsoObject.hash);
                                syncIsoObject.reqCount++;
                            }

                            if (System.currentTimeMillis() - syncIsoObject.reqTime >= this.timeout) {
                                syncIsoObject.reqTime = System.currentTimeMillis();
                                int9++;
                                byteBuffer2.putInt(syncIsoObject.x);
                                byteBuffer2.putInt(syncIsoObject.y);
                                byteBuffer2.put((byte)syncIsoObject.z);
                                byteBuffer2.putLong(syncIsoObject.hash);
                                syncIsoObject.reqCount++;
                            }

                            if (int9 >= 100) {
                                break;
                            }
                        }
                    }
                }

                if (int9 == 0) {
                    GameClient.connection.cancelPacket();
                    return;
                }

                int int11 = byteBuffer2.position();
                byteBuffer2.position(int8);
                byteBuffer2.putShort((short)int9);
                byteBuffer2.position(int11);
                PacketTypes.PacketType.SyncObjects.send(GameClient.connection);
            }
        }
    }

    public void receiveSyncIsoChunk(int x, int y) {
        synchronized (this.requestsSyncIsoChunk) {
            for (int _int = 0; _int < this.requestsSyncIsoChunk.size(); _int++) {
                ObjectsSyncRequests.SyncIsoChunk syncIsoChunk = (ObjectsSyncRequests.SyncIsoChunk)this.requestsSyncIsoChunk.get(_int);
                if (syncIsoChunk.x == x && syncIsoChunk.y == y) {
                    this.requestsSyncIsoChunk.remove(_int);
                    return;
                }
            }
        }
    }

    public void receiveSyncIsoGridSquare(int x, int y, int z) {
        synchronized (this.requestsSyncIsoGridSquare) {
            for (int _int = 0; _int < this.requestsSyncIsoGridSquare.size(); _int++) {
                ObjectsSyncRequests.SyncIsoGridSquare syncIsoGridSquare = (ObjectsSyncRequests.SyncIsoGridSquare)this.requestsSyncIsoGridSquare.get(_int);
                if (syncIsoGridSquare.x == x && syncIsoGridSquare.y == y && syncIsoGridSquare.z == z) {
                    this.requestsSyncIsoGridSquare.remove(_int);
                    return;
                }
            }
        }
    }

    public void receiveSyncIsoObject(int x, int y, int z, long hash) {
        synchronized (this.requestsSyncIsoObject) {
            for (int _int = 0; _int < this.requestsSyncIsoObject.size(); _int++) {
                ObjectsSyncRequests.SyncIsoObject syncIsoObject = (ObjectsSyncRequests.SyncIsoObject)this.requestsSyncIsoObject.get(_int);
                if (syncIsoObject.x == x && syncIsoObject.y == y && syncIsoObject.z == z && syncIsoObject.hash == hash) {
                    this.requestsSyncIsoObject.remove(_int);
                    return;
                }
            }
        }
    }

    public void receiveGridSquareHashes(ByteBuffer bb) {
        short short0 = bb.getShort();

        for (int int0 = 0; int0 < short0; int0++) {
            short short1 = bb.getShort();
            short short2 = bb.getShort();
            long _long = bb.getLong();
            short short3 = bb.getShort();

            for (int int1 = 0; int1 < short3; int1++) {
                int int2 = bb.get() + short1 * 10;
                int int3 = bb.get() + short2 * 10;
                byte _byte = bb.get();
                int int4 = bb.getInt();
                IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int2, int3, _byte);
                if (isoGridSquare != null) {
                    int int5 = isoGridSquare.getHashCodeObjectsInt();
                    if (int5 != int4) {
                        ObjectsSyncRequests.SyncIsoGridSquare syncIsoGridSquare = new ObjectsSyncRequests.SyncIsoGridSquare();
                        syncIsoGridSquare.x = int2;
                        syncIsoGridSquare.y = int3;
                        syncIsoGridSquare.z = _byte;
                        syncIsoGridSquare.reqTime = 0L;
                        syncIsoGridSquare.reqCount = 0;
                        synchronized (this.requestsSyncIsoGridSquare) {
                            this.requestsSyncIsoGridSquare.add(syncIsoGridSquare);
                        }
                    }
                }
            }

            this.receiveSyncIsoChunk(short1, short2);
        }
    }

    public void receiveGridSquareObjectHashes(ByteBuffer bb) {
        short _short = bb.getShort();

        for (int int0 = 0; int0 < _short; int0++) {
            int int1 = bb.getInt();
            int int2 = bb.getInt();
            byte byte0 = bb.get();
            this.receiveSyncIsoGridSquare(int1, int2, byte0);
            IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int1, int2, byte0);
            if (isoGridSquare == null) {
                return;
            }

            byte byte1 = bb.get();
            int int3 = bb.getInt() - 3;
            long[] long0 = new long[byte1];

            for (int int4 = 0; int4 < byte1; int4++) {
                long0[int4] = bb.getLong();
            }

            try {
                boolean[] boolean0 = new boolean[isoGridSquare.getObjects().size()];
                boolean[] boolean1 = new boolean[byte1];

                for (int int5 = 0; int5 < byte1; int5++) {
                    boolean1[int5] = true;
                }

                for (int int6 = 0; int6 < isoGridSquare.getObjects().size(); int6++) {
                    boolean0[int6] = false;
                    long long1 = isoGridSquare.getObjects().get(int6).customHashCode();
                    boolean boolean2 = false;

                    for (int int7 = 0; int7 < byte1; int7++) {
                        if (long0[int7] == long1) {
                            boolean2 = true;
                            boolean1[int7] = false;
                            break;
                        }
                    }

                    if (!boolean2) {
                        boolean0[int6] = true;
                    }
                }

                for (int int8 = isoGridSquare.getObjects().size() - 1; int8 >= 0; int8--) {
                    if (boolean0[int8]) {
                        isoGridSquare.getObjects().get(int8).removeFromWorld();
                        isoGridSquare.getObjects().get(int8).removeFromSquare();
                    }
                }

                for (int int9 = 0; int9 < byte1; int9++) {
                    if (boolean1[int9]) {
                        ObjectsSyncRequests.SyncIsoObject syncIsoObject = new ObjectsSyncRequests.SyncIsoObject();
                        syncIsoObject.x = int1;
                        syncIsoObject.y = int2;
                        syncIsoObject.z = byte0;
                        syncIsoObject.hash = long0[int9];
                        syncIsoObject.reqTime = 0L;
                        syncIsoObject.reqCount = 0;
                        synchronized (this.requestsSyncIsoObject) {
                            this.requestsSyncIsoObject.add(syncIsoObject);
                        }
                    }
                }
            } catch (Throwable throwable) {
                DebugLog.log("ERROR: receiveGridSquareObjects " + throwable.getMessage());
            }

            isoGridSquare.RecalcAllWithNeighbours(true);
            IsoWorld.instance.CurrentCell.checkHaveRoof(isoGridSquare.getX(), isoGridSquare.getY());
            bb.position(int3);
        }

        LuaEventManager.triggerEvent("OnContainerUpdate");
    }

    public void receiveObject(ByteBuffer bb) {
        int int0 = bb.getInt();
        int int1 = bb.getInt();
        byte byte0 = bb.get();
        long long0 = bb.getLong();
        this.receiveSyncIsoObject(int0, int1, byte0, long0);
        IsoGridSquare isoGridSquare = IsoWorld.instance.CurrentCell.getGridSquare(int0, int1, byte0);
        if (isoGridSquare != null) {
            byte byte1 = bb.get();
            long[] long1 = new long[byte1];

            for (int int2 = 0; int2 < byte1; int2++) {
                long1[int2] = bb.getLong();
            }

            long[] long2 = new long[isoGridSquare.getObjects().size()];

            for (int int3 = 0; int3 < isoGridSquare.getObjects().size(); int3++) {
                long2[int3] = isoGridSquare.getObjects().get(int3).customHashCode();
            }

            int int4 = isoGridSquare.getObjects().size();
            int int5 = getObjectInsertIndex(long2, long1, long0);
            if (int5 == -1) {
                DebugLog.log("ERROR: ObjectsSyncRequest.receiveObject OBJECT EXIST (" + int0 + ", " + int1 + ", " + byte0 + ") hash=" + long0);
            } else {
                IsoObject isoObject = WorldItemTypes.createFromBuffer(bb);
                if (isoObject != null) {
                    isoObject.loadFromRemoteBuffer(bb, false);
                    isoGridSquare.getObjects().add(int5, isoObject);
                    if (isoObject instanceof IsoLightSwitch) {
                        ((IsoLightSwitch)isoObject).addLightSourceFromSprite();
                    }

                    isoObject.addToWorld();
                }

                isoGridSquare.RecalcAllWithNeighbours(true);
                IsoWorld.instance.CurrentCell.checkHaveRoof(isoGridSquare.getX(), isoGridSquare.getY());
                LuaEventManager.triggerEvent("OnContainerUpdate");
            }
        }
    }

    public void serverSendRequests(UdpEngine udpEngine) {
        for (int int0 = 0; int0 < udpEngine.connections.size(); int0++) {
            this.serverSendRequests((UdpConnection)udpEngine.connections.get(int0));
        }

        synchronized (this.requestsSyncIsoGridSquare) {
            for (int int1 = 0; int1 < this.requestsSyncIsoGridSquare.size(); int1++) {
                this.requestsSyncIsoGridSquare.remove(0);
            }
        }
    }

    public void serverSendRequests(UdpConnection connection) {
        if (this.requestsSyncIsoGridSquare.size() != 0) {
            ByteBufferWriter byteBufferWriter = connection.startPacket();
            PacketTypes.PacketType.SyncObjects.doPacket(byteBufferWriter);
            byteBufferWriter.putShort((short)4);
            int int0 = byteBufferWriter.bb.position();
            byteBufferWriter.putShort((short)0);
            int int1 = 0;

            for (int int2 = 0; int2 < this.requestsSyncIsoGridSquare.size(); int2++) {
                ObjectsSyncRequests.SyncIsoGridSquare syncIsoGridSquare = (ObjectsSyncRequests.SyncIsoGridSquare)this.requestsSyncIsoGridSquare.get(int2);
                if (connection.RelevantTo((float)syncIsoGridSquare.x, (float)syncIsoGridSquare.y, 100.0F)) {
                    IsoGridSquare isoGridSquare = ServerMap.instance.getGridSquare(syncIsoGridSquare.x, syncIsoGridSquare.y, syncIsoGridSquare.z);
                    if (isoGridSquare != null) {
                        int1++;
                        byteBufferWriter.putInt(isoGridSquare.x);
                        byteBufferWriter.putInt(isoGridSquare.y);
                        byteBufferWriter.putByte((byte)isoGridSquare.z);
                        byteBufferWriter.putByte((byte)isoGridSquare.getObjects().size());
                        byteBufferWriter.putInt(0);
                        int int3 = byteBufferWriter.bb.position();

                        for (int int4 = 0; int4 < isoGridSquare.getObjects().size(); int4++) {
                            byteBufferWriter.putLong(isoGridSquare.getObjects().get(int4).customHashCode());
                        }

                        int int5 = byteBufferWriter.bb.position();
                        byteBufferWriter.bb.position(int3 - 4);
                        byteBufferWriter.putInt(int5);
                        byteBufferWriter.bb.position(int5);
                    }
                }
            }

            int int6 = byteBufferWriter.bb.position();
            byteBufferWriter.bb.position(int0);
            byteBufferWriter.putShort((short)int1);
            byteBufferWriter.bb.position(int6);
            PacketTypes.PacketType.SyncObjects.send(GameClient.connection);
        }
    }

    private class SyncIsoChunk {
        int x;
        int y;
        long hashCodeObjects;
        long reqTime;
        int reqCount;
    }

    private class SyncIsoGridSquare {
        int x;
        int y;
        int z;
        long reqTime;
        int reqCount;

        public int hashCode() {
            return this.x + this.y + this.z;
        }
    }

    private class SyncIsoObject {
        int x;
        int y;
        int z;
        long hash;
        long reqTime;
        int reqCount;

        public int hashCode() {
            return (int)((long)(this.x + this.y + this.z) + this.hash);
        }
    }
}
